package com.idearworld.controller;

import static com.idearworld.utils.ControllerUtil.failedResult;
import static com.idearworld.utils.ControllerUtil.successResult;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.idearworld.beans.GlobalConstants;
import com.idearworld.beans.QueryBean;
import com.idearworld.beans.TranslateDataWrapper;
import com.idearworld.model.BlogComment;
import com.idearworld.model.TranslateData;
import com.idearworld.service.TranslateDataService;

@Controller
@RequestMapping("/translateData")
public class TranslateDataCtrl {
	
	@Autowired
	private TranslateDataService translateDataService;

	@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	@ResponseStatus(HttpStatus.CREATED)
	@ResponseBody
	public Map<String, Object> createTranslateData(@RequestBody TranslateDataWrapper translateDataWrapper,HttpSession session) {
		
		if(translateDataWrapper == null){
			return failedResult(GlobalConstants.ERROR_TYPE_ERROR_PARAM,"Error parameter");
		}
		translateDataWrapper.getTranslateData().setCreateDate(new Date());
		TranslateData td=translateDataService.createTranslateData(translateDataWrapper.getBlogId(),translateDataWrapper.getTranslateData());
		translateDataWrapper.setTranslateData(td);
		return successResult(translateDataWrapper);
	}
	
	@RequestMapping(method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public Map<String, Object> queryBlogTranslateData(@RequestBody QueryBean queyBean,HttpSession session) {
		List<TranslateData> datas=translateDataService.queryTranslateData(queyBean);
		return successResult(datas);
	}
	@RequestMapping(value="{id}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public Map<String, Object> getNewWords(@PathVariable( "id" ) final Integer blogId,HttpSession session) {
		QueryBean queryBean=new QueryBean();
		queryBean.setBlogId(blogId);
		List<TranslateData> datas=translateDataService.queryTranslateData(queryBean);
		return successResult(datas);
	}

}
